package spec.test;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;


import br.mia.unifor.crawler.builder.ComputeProvider;
import br.mia.unifor.crawler.executer.Execution;
import br.mia.unifor.crawler.executer.artifact.Metric;
import br.mia.unifor.crawler.executer.artifact.MetricEval;
import br.mia.unifor.crawler.executer.artifact.Scenario;
import br.mia.unifor.crawler.executer.artifact.Scriptlet;

public class SpecExecution extends Execution {

	private Logger logger = Logger.getLogger(getClass().getName());
	private Double resultValue = null;

	public SpecExecution(Scenario scenario) {
		super(scenario);
	}

	@Override
	public void testRun(String workload) throws Exception {
		long time = System.currentTimeMillis();
		List scripts = new ArrayList<String>(2);
		scripts.add("~/executeSpec.sh "+workload+" "+time);
		//java -jar ~/SPECjvm2008/SPECjvm2008.jar -wt 5s -it 5s -bt 4 compress /tmp/time
		//java -jar ~/SPECjvm2008/SPECjvm2008.jar -wt 5s -it 5s -bt 4 -ikv compress
		//java -jar ~/SPECjvm2008/SPECjvm2008.jar -wt 5s -it 5s -bt 4 -ikv compress >> /tmp/time
		
		logger.info("the log file is /tmp/"+time);
		
		Scriptlet script = new Scriptlet();
		script.setScripts(scripts);
		
		String resultado = ComputeProvider.runScript(scenario.getApplication().getVirtualMachines().get(0), script, scenario.getApplication().getVirtualMachines().get(0).getPublicIpAddress(), logger); 
		
		resultValue = evalResult(resultado);
		
		logger.info("Result => "+resultValue);

	}
	
	public Double evalResult(String result){
		//Noncompliant composite result: 23.25 ops/m
		String value =  result.substring(result.indexOf(":")+1,result.length()-6);
		logger.info("Result String VALUE "+value);
		
		return new Double(value);
	}

	@Override
	public boolean isRunning() {
		return false;
	}

	@Override
	public List<MetricEval> getResult() {
		
		List<MetricEval> metrics = new ArrayList<MetricEval>();
		for (Metric metric : super.scenario.getMetrics()) {
			MetricEval eval = new MetricEval(metric);
			eval.setPassed(Boolean.TRUE);
			metrics.add(eval);
			metrics.get(metrics.size()-1).setValue(resultValue);
		}
		
		resultValue = null;
		
		return metrics;
	}
	
   /*public static void main(String[] args) throws Exception {
	   Scenario scenario = new Scenario();
	   ApplicationImpl app = new ApplicationImpl();
	   List<VirtualMachine> virtualMachines = new ArrayList<VirtualMachine>(1);
	   VirtualMachine machine = new VirtualMachine();
	   machine.setPublicIpAddress(args[0]);
	   
	   virtualMachines.add(machine);
	   
	   scenario.setApplication(app);
	   app.setVirtualMachines(virtualMachines);
	   
	   List<Metric> metrics = new ArrayList<Metric>(1);
	   Metric metric = new Metric();
	   metric.setName("Ops per second");
	   metrics.add(metric);
	   
	   scenario.setMetrics(metrics);
	   
	   
	   SpecExecution spec = new SpecExecution(scenario);
	   spec.testRun("compress");
	   
	
   }*/

}
